const { observable, action, runInAction } = require("mobx-miniprogram")
const { after } = require("underscore")

const store = observable({
  // 数据字段
  list: [],

  get markers() {
    const markers = this.list.map( ({ id, latitude, longitude, type }) => {
      return {
        id,
        latitude,
        longitude,
        iconPath: `/resources/${type}.png`,
        width: '60rpx',
        height: '60rpx'
      }
    })
    return markers
  },

  get item() {
    return (id) => {
      return this.list.find(v => v.id === id)
    }
  },


  // actions
  loadData: action(function() {
    // 拉取地图数据
    const db = wx.cloud.database()
    db.collection('minipet')
      .get({
        success: (res) => {
          
          // 异步数据更新
          runInAction(() => {
            this.list = res.data
          })
        }
    })
  })
})

module.exports = store